package org.example.route.service.impl;

import org.example.route.constant.StationRouteConstant;
import org.example.route.constant.TrainConstant;
import org.example.route.mapper.*;
import org.example.route.model.dto.BaseRequest;
import org.example.route.model.dto.RouteDTO;
import org.example.route.model.dto.RouteQueryDTO;
import org.example.route.model.dto.StationRouteDTO;
import org.example.route.model.pojo.Route;
import org.example.route.model.pojo.StationRoute;
import org.example.route.model.pojo.Train;
import org.example.route.model.vo.RouteVO;
import org.example.route.model.vo.StationRouteVO;
import org.example.route.service.RouteService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class RouteServiceImpl implements RouteService {

    @Autowired
    private RouteMapper routeMapper;
    @Autowired
    private StationRouteMapper stationRouteMapper;
    @Autowired
    private StationMapper stationMapper;
    @Autowired
    private TrainMapper trainMapper;
    @Autowired
    private PriceMapper priceMapper;

    DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    @Override
    public void add(RouteDTO routeDTO) {
        Route route = new Route();
        BeanUtils.copyProperties(routeDTO,route);
        routeMapper.add(route);//添加路线的名称
        Long id = route.getId();
        List<StationRouteDTO> stationRoutes = routeDTO.getStationRoutes();
        //修改列车的运行状态
        Long trainId = stationRoutes.get(0).getTrainId();
        for (StationRouteDTO stationRouteDTO : stationRoutes) {
            StationRoute stationRoute = new StationRoute();
            BeanUtils.copyProperties(stationRouteDTO,stationRoute);
            stationRoute.setRouteId(id);
            stationRoute.setStatus(StationRouteConstant.STATUS_NORMAL);
            stationRouteMapper.add(stationRoute);
        }

        Train train = trainMapper.getById(trainId);
        train.setStatus(TrainConstant.STATUS_RUNNING);
        trainMapper.update(train);

    }

    @Override
    public List<RouteVO> list(RouteQueryDTO routeQueryDTO) {

        List<Route> routeList = routeMapper.list(routeQueryDTO.getName());
        List<RouteVO> routeVOList = new ArrayList<>();

        for (Route route : routeList) {
            RouteVO routeVO = new RouteVO();
            List<StationRoute> stationRouteList = stationRouteMapper.list(route.getId(),routeQueryDTO.getStationId(),routeQueryDTO.getTrainId());
            if(stationRouteList.isEmpty())
            {
                continue;
            }
            List<StationRouteVO> stationRouteVOList = new ArrayList<>();
            for (StationRoute stationRoute : stationRouteList)
            {
                StationRouteVO stationRouteVO = new StationRouteVO();
                BeanUtils.copyProperties(stationRoute,stationRouteVO);
                stationRouteVO.setArrivalTime(stationRoute.getArrivalTime().format(sdf));
                stationRouteVO.setGoTime(stationRoute.getGoTime().format(sdf));
                stationRouteVO.setStationName(stationMapper.getNameById(stationRoute.getStationId()));
                stationRouteVOList.add(stationRouteVO);
            }

            routeVO.setId(route.getId());
            routeVO.setName(route.getName());
            routeVO.setStationRoutes(stationRouteVOList);
            routeVO.setTrainSerialNumber(trainMapper.getSerialById(stationRouteList.get(0).getTrainId()));

            routeVOList.add(routeVO);


        }


              return routeVOList;

    }


    @Override
    public void delete(BaseRequest baseRequest) {
        Route route = routeMapper.getById(Long.parseLong(baseRequest.getKey()));
        if (Objects.isNull( route)){
            throw new RuntimeException("路线不存在");
        }

        //查询票价信息，是否已经绑定
        List<StationRoute> stationRoutes = stationRouteMapper.getByRouteId(route.getId());

        for (StationRoute stationRoute : stationRoutes) {

               if (!priceMapper.getByGoId(stationRoute.getId()).isEmpty()){
                   throw new RuntimeException("该路线有票价信息，请先删除票价信息");
               }

        }

        //先删除路线节点信息，再删除路线信息
        stationRouteMapper.deleteAllByRouteId(route.getId());

        routeMapper.delete(route.getId());




    }

    @Override
    public void update(RouteDTO routeDTO) {




    }
}
